package com.china08.yxyapi.api.manage.sch.platform.website.setting;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class SwsSettingDoaminDetailController extends YxyApiBaseController {

    @Autowired
    SwsSettingDomainsRepository swsSettingDomainsRepository;

    @Value(value = "${yxy.sws.url.suffix}")
    private String suffix;

    @ApiOperation("获取学校网址")
    @RequestMapping(value = { "/api_yw/manage/sch/platform/website/setting/doamin/detail", "/api_yw/manage/sch/platform/setting/doamin/detail" }, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) ReqModel reqModel) {

        String schoolId = getProfile(request, response).getSchoolId();
        Assert.isTrue(StringUtils.isNotBlank(schoolId), "学校ID为空");

        RespModel respModel = new RespModel();
        respModel.suffix = suffix;

        // 学校所有域名
        List<SwsSettingDomains> swsSettingDomainsList = swsSettingDomainsRepository.findBySchoolId(schoolId);
        if (swsSettingDomainsList.size() > 0) {
            SwsSettingDomains swsSettingDomains = swsSettingDomainsList.get(0);
            respModel.domainId = swsSettingDomains.getId();
            // 有后缀的： 显示使用
            respModel.domainName = swsSettingDomains.getDomainName();
            // 没有后缀的: 修改使用
            String domainNameNoSuffix = swsSettingDomains.getDomainName();
            domainNameNoSuffix = StringUtils.replace(domainNameNoSuffix, suffix, "");
            respModel.domainNameNoSuffix = domainNameNoSuffix;
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

    }

    protected static class RespModel extends YxyApiReqModel {
        public String id;
        public String domainId;
        // 有后缀
        public String domainName;
        // 没有后缀
        public String domainNameNoSuffix;
        public String suffix;
    }
}
